package org.example.src.service.impl;

import org.example.src.dto.request.FeedbackRequest;
import org.example.src.dto.response.FeedbackResponse;
import org.example.src.entity.*;
import org.example.src.repository.*;
import org.example.src.service.FeedbackService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackRepository feedbackRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private TeacherRepository teacherRepository;

    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private ApplicationRepository applicationRepository;

    @Override
    public List<Feedback> findAllFeedbacks() {
        return feedbackRepository.findAll();
    }

    @Override
    public Feedback findFeedbackById(Long id) {
        return feedbackRepository.findById(id).orElse(null);
    }

    @Override
    public Feedback createFeedback(FeedbackRequest request) {
        Feedback feedback = new Feedback();
        feedback.setContent(request.getContent());
        feedback.setRating(request.getRating());

        try {
            feedback.setType(FeedbackType.valueOf(request.getType()));
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("无效的反馈类型");
        }

        User fromUser = userRepository.findById(request.getFromUserId())
                .orElseThrow(() -> new RuntimeException("发送用户未找到"));
        feedback.setFromUser(fromUser);

        User toUser = userRepository.findById(request.getToUserId())
                .orElseThrow(() -> new RuntimeException("接收用户未找到"));
        feedback.setToUser(toUser);

        if (request.getStudentId() != null) {
            Student student = studentRepository.findById(request.getStudentId())
                    .orElseThrow(() -> new RuntimeException("学生未找到"));
            feedback.setStudent(student);
        }

        if (request.getTeacherId() != null) {
            Teacher teacher = teacherRepository.findById(request.getTeacherId())
                    .orElseThrow(() -> new RuntimeException("教师未找到"));
            feedback.setTeacher(teacher);
        }

        if (request.getCompanyId() != null) {
            Company company = companyRepository.findById(request.getCompanyId())
                    .orElseThrow(() -> new RuntimeException("公司未找到"));
            feedback.setCompany(company);
        }

        if (request.getApplicationId() != null) {
            Application application = applicationRepository.findById(request.getApplicationId())
                    .orElseThrow(() -> new RuntimeException("申请未找到"));
            feedback.setApplication(application);
        }

        return feedbackRepository.save(feedback);
    }

    @Override
    public void deleteFeedback(Long id) {
        feedbackRepository.deleteById(id);
    }

    @Override
    public List<Feedback> findFeedbacksByStudentId(Long studentId) {
        return feedbackRepository.findByStudentId(studentId);
    }

    @Override
    public List<Feedback> findFeedbacksByTeacherId(Long teacherId) {
        return feedbackRepository.findByTeacherId(teacherId);
    }

    @Override
    public List<Feedback> findFeedbacksByCompanyId(Long companyId) {
        return feedbackRepository.findByCompanyId(companyId);
    }

    @Override
    public List<Feedback> findFeedbacksByFromUserId(Long userId) {
        return feedbackRepository.findByFromUserId(userId);
    }

    @Override
    public List<Feedback> findFeedbacksByToUserId(Long userId) {
        return feedbackRepository.findByToUserId(userId);
    }

    @Override
    public List<FeedbackResponse> getFeedbacksByToUserId(Long userId) {
        List<Feedback> feedbacks = feedbackRepository.findByToUserId(userId);
        return feedbacks.stream().map(this::convertToResponse).collect(Collectors.toList());
    }

    private FeedbackResponse convertToResponse(Feedback feedback) {
        FeedbackResponse response = new FeedbackResponse();
        BeanUtils.copyProperties(feedback, response);
        response.setType(feedback.getType().name());

        if (feedback.getFromUser() != null) {
            response.setFromUserId(feedback.getFromUser().getId());
            response.setFromUserName(feedback.getFromUser().getUsername());
        }

        if (feedback.getToUser() != null) {
            response.setToUserId(feedback.getToUser().getId());
            response.setToUserName(feedback.getToUser().getUsername());
        }

        if (feedback.getStudent() != null) {
            response.setStudentId(feedback.getStudent().getId());
            response.setStudentName(feedback.getStudent().getFullName());
        }

        if (feedback.getTeacher() != null) {
            response.setTeacherId(feedback.getTeacher().getId());
            response.setTeacherName(feedback.getTeacher().getFullName());
        }

        if (feedback.getCompany() != null) {
            response.setCompanyId(feedback.getCompany().getId());
            response.setCompanyName(feedback.getCompany().getCompanyName());
        }

        return response;
    }
}
